import { store } from '@/store'
import { defineStore } from 'pinia'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
import * as UserApi from '@/api/member/user'

const { wsCache } = useCache()

interface UserInfoVO {
  // USER 缓存
  isSetUser: boolean
  user: UserApi.UserVO
}

export const useShopUserStore = defineStore('shop-user', {
  state: (): UserInfoVO => ({
    isSetUser: false,
    user: {
      id: 0,
      avatar: '',
      nickname: '',
      birthday: undefined,
      createTime: undefined,
      loginDate: undefined,
      loginIp: '',
      mark: '',
      mobile: '',
      name: undefined,
      registerIp: '',
      sex: 0,
      status: 0,
      areaId: undefined,
      areaName: undefined,
      levelName: null,
      point: undefined,
      totalPoint: undefined,
      experience: undefined
    }
  }),
  getters: {
    getIsSetUser(): boolean {
      return this.isSetUser
    },
    getUser(): UserApi.UserVO {
      return this.user
    }
  },
  actions: {
    async setUserInfoAction(user: UserApi.UserVO) {
      this.user = user
      this.isSetUser = true
      wsCache.set(CACHE_KEY.SHOP_USER, user)
    },

    resetState() {
      this.isSetUser = false
      this.user = {
        id: 0,
        avatar: '',
        nickname: '',
        birthday: undefined,
        createTime: undefined,
        loginDate: undefined,
        loginIp: '',
        mark: '',
        mobile: '',
        name: undefined,
        registerIp: '',
        sex: 0,
        status: 0,
        areaId: undefined,
        areaName: undefined,
        levelName: null,
        point: undefined,
        totalPoint: undefined,
        experience: undefined
      }
    }
  }
})

export const useShopUserStoreWithOut = () => {
  return useShopUserStore(store)
}
